<?php
/**
 * $Id$
 */

/**
 * 登陆动作
 *
 * @package App
 */
class App_Login extends App_Abstract_User implements App_Interface_Do
{
	public function action()
	{
		if($this->user->logined()) {
			$this->response->redirect($this->config->index);
		}

		$validator = new Tinyask_Validate();
		$validator->addRule('name', 'required', -t('请输入用户名'));
		$validator->addRule('password', 'required', _t('请输入密码'));

		/** 截获验证异常 **/
		if(($error = $validator->run($this->request->from('name', 'password')))) {
			/** 设置提示信息 **/
			$this->app('App_Notice')->set($error);
			$this->response->goBack();
		}

		$user = $this->db->fetchObject($this->select()->where('name = ?', $this->request->name)->limit(1));

		if($user && $user->password == md5(md5($this->request->password) . $user->mixed)) {
			$this->user->login($user->uid, $this->request->remember == 1 ? $this->config->gmtTime + $this->config->timezone + 30 * 24 * 3600 : 0);
		} else {
			$this->app('App_Notice')->set(_t('用户名或密码无效'), null, 'error');
			$this->response->redirect($this->config->loginUrl . ($this->request->referer === null ? null : '?referer=' . urlencode($this->request->referer)));
		}

		if($this->request->referer != null) {
			$this->response->redirect($this->request->referer);
		} else {
			$this->reponse->redirect($this->config->adminUrl);
		}
	}
}
